package com.zhangmeng.online.exam.controller;

import com.zhangmeng.online.exam.dao.QuestionDao;
import com.zhangmeng.online.exam.dao.SubjectDao;
import com.zhangmeng.online.exam.dto.Result;
import com.zhangmeng.online.exam.entity.Permission;
import com.zhangmeng.online.exam.entity.Question;
import com.zhangmeng.online.exam.entity.Subject;
import com.zhangmeng.online.exam.entity.User;
import com.zhangmeng.online.exam.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zm
 * @date 2025/3/7 9:49
 * @version: 1.0
 */
@RestController
@RequestMapping("/question")
public class QuestionController {

    @Autowired
    private QuestionDao questionDao;

    @Autowired
    private SubjectDao subjectDao;

    @RequestMapping("/list")
    public Result list(Integer pageNum, Integer pageSize) {
        Pageable pageable = PageRequest.of(PageUtils.getPageNum(pageNum), pageSize);
        Page<Question> all = questionDao.findAll(pageable);
        List<Question> list = all.getContent();
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (Question question : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", question.getId());
            map.put("name", question.getName());
            map.put("type", question.getType().getDesc());
            map.put("subject", question.getSubject().getName());
            map.put("score", question.getScore());
            resultList.add(map);
        }
        return Result.success(pageNum, pageSize, all.getTotalElements(), resultList);
    }

    @RequestMapping("/saveQuestion")
    public Result saveQuestion(@RequestBody @RequestParam  Map<String, Object> params) {
        String id =  params.get("id").toString();
        Question question = null;
        if (id.equals("0")){
           question = new Question();
        }else {
            question = questionDao.getById(Long.parseLong(id));
        }

        question.setName((String) params.get("question"));
        question.setType(Enum.valueOf(Question.Type.class, (String) params.get("type")));
        question.setScore(Integer.parseInt((String) params.get("score")));
        question.setExplanation((String) params.get("explanation"));
        Subject subject = this.subjectDao.getById(Long.parseLong((String)  params.get("subject_id")));
        question.setSubject(subject);
        questionDao.save(question);

        return Result.success(null);
    }



    @RequestMapping("/delete")
    public Result delete(Long id) {
        questionDao.deleteById(id);
        return Result.success(null);
    }

    @RequestMapping("/getAllQuestionType")
    public Result getAllQuestionType() {
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (Question.Type type : Question.Type.values()) {
            Map<String, Object> map = new HashMap<>();
            map.put("index", type.getIndex());
            map.put("desc", type.getDesc());
            map.put("type", type);
            resultList.add(map);
        }
        return Result.success(resultList);
    }


    @RequestMapping("/update")
    public Result update(@RequestBody @RequestParam  Map<String, Object> params) {
        Question question = questionDao.getById(Long.parseLong((String) params.get("id")));
        String fieldName = (String) params.get("fieldName");
        String oldValue = (String) params.get("oldValue");
        String newValue = (String) params.get("newValue");

        if (fieldName.equals("name")){
            question.setName(newValue);
        }
        if (fieldName.equals("score")){
            question.setScore(Integer.parseInt(newValue));
        }
        if (fieldName.equals("explanation")){
            question.setExplanation(newValue);
        }
        questionDao.save(question);
        return Result.success(null);
    }

    @RequestMapping("/getQuestionById")
    public Result getQuestionById(Long id) {
        Question question = questionDao.getById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("id", question.getId());
        map.put("name", question.getName());
        map.put("type", question.getType().getDesc());
        map.put("subject", question.getSubject().getName());
        map.put("score", question.getScore());
        map.put("explanation", question.getExplanation());
        return Result.success(map);
    }


}
